package com.czmec.miaosha.config;

import com.czmec.miaosha.access.UserContext;
import com.czmec.miaosha.controller.LoginController;
import com.czmec.miaosha.domain.MiaoshaUser;
import com.czmec.miaosha.service.MiaoshaUserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// 自定义参数解析器
@Service
public class UserArgumentResolver implements HandlerMethodArgumentResolver {
	private static Logger log = LoggerFactory.getLogger(UserArgumentResolver.class);

	@Autowired
    MiaoshaUserService userService;
	
	public boolean supportsParameter(MethodParameter parameter) {
		Class<?> clazz = parameter.getParameterType();
		return clazz==MiaoshaUser.class;
	}

	public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
			NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
		MiaoshaUser user = UserContext.getUser();
		if (user != null) {
			log.info(user.toString());
		}
		return user;
	}

}
